package com.ipedg.minecraft.SQL;

import com.ipedg.minecraft.main;
import org.bukkit.command.CommandSender;
import java.sql.*;

public class MySQLManager {
    public static MySQLManager instance = null;
    private static Connection connection = null;
    public static MySQLManager get() {
        return instance == null ? instance = new MySQLManager() : instance;
    }

    public void enableMySQL()
    {
        connectMySQL();
        String cmd = SQLCommand.CREATE_player.commandToString();
        try {
            PreparedStatement ps = connection.prepareStatement(cmd);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void connectMySQL()
    {
        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:"+ main.plugin.getConfig().getString("dbname")+".db");
        } catch (SQLException | ClassNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

    public void doCommand(PreparedStatement ps, CommandSender sender)
    {
        try {
            ps.executeUpdate();
        } catch (SQLException e) {
            sender.sendMessage("执行指令失败，以下为错误提示");
            e.printStackTrace();
        }
    }

    public void shutdown() {
        try {
            connection.close();
        } catch (SQLException e) {
            //断开连接失败
            e.printStackTrace();
        }
    }
//玩家
    public void AddPlayer(String UUID,String PlayerName, CommandSender sender) {
        try {
            PreparedStatement ps;
            String s = SQLCommand.ADD_player.commandToString();
            ps = connection.prepareStatement(s);
            //ps.setInt(1, Integer.parseInt(data1));
            ps.setString(1, UUID);
            ps.setString(2, PlayerName);
            doCommand(ps, sender);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            sender.sendMessage("输入的不是整数，插入失败");
        }
    }

    public void DeletePlayer(String UUID,CommandSender sender){
        try{
            PreparedStatement ps;
            String s = SQLCommand.DELETE_player.commandToString();
            ps = connection.prepareStatement(s);
            ps.setString(1,UUID);
            doCommand(ps,sender);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void FindPlayer(String UUID,CommandSender sender){
        try{
            String s = SQLCommand.SELECT_player.commandToString();
            PreparedStatement ps = connection.prepareStatement(s);
            ps.setString(1,UUID);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()){
                String info = resultSet.getString("info");
                sender.sendMessage(info);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            sender.sendMessage("输入有误");
        }
    }

    //签到
    public void GetMonthRecord(String UUID,CommandSender sender){
        try{
            String s = SQLCommand.SELECT_activeness.commandToString();
            PreparedStatement ps = connection.prepareStatement(s);
            ps.setString(1,UUID);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()){
                String info = resultSet.getString("info");
                sender.sendMessage(info);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            sender.sendMessage("输入有误");

        }
    }
}
